define(function(require) {
	var $ = require("jquery");
	var justep = require("$UI/system/lib/justep");
	require("$UI/system/lib/cordova/cordova");
	require("cordova!com.justep.cordova.plugin.barcodescanner"); 
	var Model = function() {
		console.log("Delivery.js Model()"); 
		this.callParent();
	};

	Model.prototype.CustomerApi = function() {
		console.log("CustomerApi()");
		var api_url = window.app.base_url + window.app.CustomerApi;
		var data = {};
		data.username = window.app.username;
		data.password = window.app.password;
		data.CtmType = window.app.CtmType;
		var self = this;
		$.ajax({
			type : "POST",
			url : require.toUrl(api_url),
			data : {
				'data' : JSON.stringify(data)
			},
			dataType : 'json',
			success : function(res) {
				console.log(res);
				window.app.CustomerApi_ret = res;
				self.comp("FenXiaoShangData").clear();
				self.comp("FenXiaoShangData").loadData(res);
				console.log(self.comp("FenXiaoShangData"));
				return;
			},
			error : function(e) {
				return;
			}
		});
	};

	Model.prototype.modelLoad = function(event) {
		console.log("modelLoad()");
	};
	Model.prototype.modelParamsReceive = function(event) {
		console.log("modelParamsReceive()");
		// 初始化
		this.comp("FenXiaoShangData").clear();
		// 获取要展示的数据
		this.CustomerApi();
		// 初始化时间
		var date_string = justep.Date.toString(new Date(), "yyyyMMdd");
		this.getElementByXid("Date_Label").innerHTML = date_string;

	};
	Model.prototype.Next_ButtonClick = function(event) {
		console.log("Next_ButtonClick()");
		console.log(this.comp("FenXiaoShang_Select").val());
		if (this.comp("FenXiaoShang_Select").val().length < 1) {
			justep.Util.hint("请先选择分销商");
			return;
		}
		this.CreateMainFranchiser();
	};
	Model.prototype.CreateMainFranchiser = function() {
		console.log("CreateMainFranchiser()");

		console.log(window.app.UserApi_ret);
		var api_url = window.app.base_url + window.app.FranchiserInsertApi;
		var data = {};
		data.username = window.app.username;
		data.password = window.app.password;
		data.ctmnoone = window.app.UserApi_ret.CtmNo;
		data.ctmnotwo = this.comp("FenXiaoShang_Select").val();
		data.deliverydate = this.getElementByXid("Date_Label").innerHTML;
		data.apply = window.app.UserApi_ret.Contact;
		data.CtmType = window.app.CtmType;

		console.log(data);
		var self = this;
		$.ajax({
			type : "POST",
			url : require.toUrl(api_url),
			data : {
				'data' : JSON.stringify(data)
			},
			dataType : 'json',
			success : function(res) {
				console.log(res);
				window.app.FranchiserInsertApi_ret = res;
				var params = {};
				params.FID = res[0].ID;
				params.SN = res[0].SN;
				params.PrdArray = [];
				self.getParent().comp("AddProducts_Window").load("AddProducts.w", params);
				self.getParent().comp("Main_Contents").to("AddProducts_Content");
				return;
			},
			error : function(e) {
				return;
			}
		});
	};
	Model.prototype.FaHuoShiJian_RowClick = function(event) {
		console.log("FaHuoShiJian_RowClick");
		var datePicker = this.comp("datePicker");
		datePicker.set('type', 'date');
		datePicker.min = new Date();
		datePicker.setValue(new Date());
		datePicker.show();
	};
	/**
	 * 日期选择完毕执行的回调函数
	 * 
	 * @param event
	 */
	Model.prototype.DateSelectFinish = function(event) {
		console.log("DateSelectFinish");
		var comp = event.source;
		var value = comp.getValue();
		if (value instanceof Date) {
			value = justep.Date.toString(value, "yyyyMMdd");
		} else {
			justep.Util.hint("日期组件错误");
			value = "";
		}
		this.getElementByXid("Date_Label").innerHTML = value;
		console.log(this.getElementByXid("Date_Label").innerHTML);
	};

	Model.prototype.DirectSellBtnClick = function(event) {
		this.getParent().comp("DirectSell_Window").load("DirectSell.w");
		this.getParent().comp("Main_Contents").to("DirectSellContent");
	};                          

	Model.prototype.FactoryBtnClick = function(event){
		this.getParent().comp("Factory_Window").load("Factory/Factory.w");
		this.getParent().comp("Main_Contents").to("Factory_Content");
	};                          

	return Model;
});